package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.woniu_bx.mapper.PermMapper;
import com.woniu.woniu_bx.pojo.Perm;
import com.woniu.woniu_bx.service.PermService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:GuoHai
 * @program: IntelliJ IDEA
 * @data: 2022/8/27 16:20
 */
@Service
@Transactional
public class PermServiceImpl extends ServiceImpl<PermMapper, Perm> implements PermService {
    @Resource
    private PermMapper permMapper;

    //根据角色id查询其所有权限（未分好父子层级）
    @Override
    public List<Perm> findPermsByRoleId(int id) {
        List<Perm> perms = permMapper.getListByRoleID(id);
        return perms;
    }

    //查询所有的权限（已经分好父子层级）
    @Override
    public List<Perm> permList() {
        List<Perm> perms = permMapper.findAll();
        for(int i = 0 ; i < perms.size() ; i ++){
            List<Perm> subPerms = new ArrayList<Perm>();
            if(perms.get(i).getPid()==null){
                for(int j = 0 ; j < perms.size() ; j++){
                    if(perms.get(j).getPid()==perms.get(i).getId()){
                        subPerms.add(perms.get(j));
                        perms.remove(j);
                        j--;
                    }
                }
            }
            perms.get(i).setSubPerm(subPerms);
        }
        return perms;
    }
}
